<?php
# User session management
require_once('includes/user_manage.php');

if (!$user->id) {
?>
<div class="note">
	Antes de pasar a realizar el pedido ha de entrar como usuario o crearse una cuenta.
</div>
<?php
	include($server->get_path_name('login.php'));
}
elseif ($user->cfm_key) {
?>
<p>Se requiere la clave de confirmación de cuenta.</p>
<p>Por favor introduce la clave que has recibido en tu email.</p>
<?php
	if (MODE == "test") {
?>
	<p>	Clave: <?php echo $user->cfm_key; ?></p>
<?php
	}
?>
<?php
	include($server->get_path_name("cfmkey_form.php"));
}
else {
	$myOrder=json_decode($_POST["serOrder"]);	// The json version of the javascript myOrder object
	$user->order=new order();
	$user->order->user_id=$user->id;
	$user->order->set_from_array($myOrder);
	// Items has been stablished from instruction above as an array of generic stdClass objects,
	// that's not ok they must been settled as an order_item class:
	for($i = 0; $i < count($user->order->items); $i++) {
		$order_item= new order_item();
		$order_item->set_from_array($user->order->items[$i]);
		$user->order->items[$i]=$order_item;
	}
	$user->address=new address();
	if ($user->address_id) {
		$user->address->id=$user->address_id;
		$user->address->db_load();
	}
	$_SESSION["ser_user"]=serialize($user);
?>
<div class="note">
	Procesando nuevo pedido.
</div>
<div id="big_cart">
	<table class="formtable order">
		<tr>
			<th class="product">Producto</th>
			<th class="quantity">Cantidad</th>
			<th class="price">Precio</th>
			<th class="total">SubTotal </th>
		</tr>
		<tr>
		      <td colspan="4" class="hr"></td>
		</tr>
		<?php foreach($user->order->items as $item) { ?>
		<tr class="product">
			<td class="product">
				<?php echo $item->name; ?>
			</td>
			<td class="quantity" style="text-align:center">
				<?php echo $item->quantity; ?>
			</td>
			<td class="price" style="text-align:right"><?php echo number_format($item->price, 2, ',', ' '); ?> €</td>
			<td class="total" style="text-align:right"><?php echo number_format($item->price * $item->quantity, 2, ',', ' '); ?> €</td>
		</tr>
		<?php } ?>
		<tr>
			<td class="hr" colspan="4"></td>
		</tr>
		<tr class="total">
			<td class="total" colspan="3">SubTotal:</td>
			<td><?php echo number_format($user->order->price, 2, ',', ' '); ?> €</td>
		</tr>
		<tr>
		      <td colspan="4" class="hr"></td>
		</tr>
	</table>
</div>
<div class="space"></div>
<?php
	include($server->get_path_name('check_out_form.php'));
}
?>
